define(["require",
'config/appregister',
'business/postbar/service/postbarhttp',
'business/services/moreHttpServices',
"business/home/directives/editorDirective",
"jquery", "business/services/filter"], function(require, app, httpServices, moreHttpServices) {
	app.controller('postbardetaCtrl', function($scope, $rootScope, $state, $stateParams) {
		$scope.$parent.isDetailPage = true;
		$scope.$parent.isMainpage = false;
		$scope.editorContent='';
		if(!angular.isUndefined($stateParams.postbarId)) {
			var theid = $stateParams.postbarId;
		}
		$scope.thePostbarId = $stateParams.postbarId;
		//头部路径跳转
		$scope.goToOtherPage = function(a) {
			if(a===1) {
				$state.go("indexCtrl");
			}
			if(a===2) {
				$state.go("eng.wikilist");
			}
		};
		//跳转到提问页
		$scope.gotopage = function() {
			$state.go("eng.iask");
		};
		//关闭收藏成功提示
		$scope.onisok = function(){
			$("#collectionisok").hide();
		};
		//跳转到我的收藏
		$scope.gotosclb = function(){
			$state.go("prosonMine.myCollection");
		};
		//点击回复的用户的头像动作
		$scope.clickpic = function(userId) {
			$state.go("othersMainPage",{userId:userId});
		};
		//标签跳搜索页
		$scope.toseach = function() {
			$state.go("search.question");
//			sessionStorage.setItem("searchKeyWord",a);
		};
		//分享动作
		$scope.shareBtnr = function() {
			$(".isay-div2").css({
				"background":"url('them/img/weibo.png') 0px 5px no-repeat",
			});
		};
		$scope.shareBtnc = function() {
			$(".isay-div2").css({
				"background":"url('them/img/isaydiv2.png') 0px 8px no-repeat",
			});
		};
		$scope.shareBtn = function() {
			
		}
		//用户跳转
		$scope.goOthers = function(userId) {
			$state.go("othersMainPage",{userId:userId});
		};
		
		$scope.toTheOther = function(userId) {
			$state.go("othersMainPage",{userId:userId});
		};
		
		//关闭问题
		$scope.no = function() {
			var req = {
				type:"GET",
				uri:'close',
				data:{
					postbarId:$stateParams.postbarId
				}
			};
			httpServices.postbarlistHttpService(req).success(function(data){
				if(!data)return;
				if(data.code==0){
					$scope.$apply(function(){
						$(".flags-none").unbind();
						$(".flags-none-div").hide();
						$(".flags-none").html("问题已关闭").css({
							"color": "#999",
							"border": "none"
						});	
						$(".answer-input-box").hide();
						
					})
				}else {
					alert(data.msg);
				}
			}).error(function(){
				alert("关闭回答请求失败");
			});
		};
		//修改回答 
		$scope.subanswer2 = function() {
			$scope.editorContent=$scope.editorContent.replace(/<.*?>/ig,"");
			if($scope.editorContent.length == 0) {
				alert("回答不能为空！");
				return;
			}else {
				var thecontent = $scope.editorContent;
				if(thecontent.length > 4000) {
					alert("最多只能输入2000个字，您输入的内容超出了限制，请做删减");
					return;
				}

				var req = {
					type:'POST',
					uri:'updateReply',
					data:{
						replyId: $scope.replyId,
						postbarId: $scope.postbarId,
						content: thecontent,
						seed:localStorage.seed,
						authToken: localStorage.authToken						
					}
				};
				$.ajax({
					type:'post',
					dataType:'json',
					data:req.data,
					url:"http://"+allUrl+":8086/elink_postbar/api/reply/updateReply",
					success:function(data) {
						if(!data)return;
							$scope.$apply(function(){
								if(data.code==0) {
									alert("提交回答成功！");
									getinfor();					
								}else {
									alert(data.msg);
								}
							});					
						},
					error:function() {
						alert("修改请求失败");
					}
				});							
			}
		};	
		$scope.noxbtn = function() {
			$(".tip-box").hide();
		}
		$scope.tologin = function() {
			$state.go("login");
		};
		$scope.toreg = function() {
			$state.go("login",{type:"register"});
		};

		$scope.fontNum=function(){
			$scope.editorContent=$scope.editorContent.replace(/<.*?>/ig,"");
			if($scope.editorContent.length>2000){
				$(".numTip").find('span').eq(0).html('字数超出').addClass('active');
			}else{
				$(".numTip").find('span').eq(0).html($scope.editorContent.length).removeClass('active');
			}
		};
		//提交回答
		$scope.subanswer = function() {
			$scope.editorContent=$scope.editorContent.replace(/<.*?>/ig,"");
			if($rootScope.userLoginInfo.logged) {
				
			}else {
				$(".tip-box").show();
				return;
			}			
			if($scope.editorContent.length > 2000) {
				alert("最多只能输入2000个字，您输入的内容超出了限制，请做删减");
				return;
			}
			
			if($scope.editorContent.length==0){
				alert("回答不能为空！");
				return;
			}
			var req = {
				type:'POST',
				uri:'addReply',
				data:{
					postbarId: $scope.postbarId,
					content: $scope.editorContent,
					seed:localStorage.seed,
					authToken: localStorage.authToken
				}
			};
			
			$.ajax({
				type:'post',
				dataType:'json',
				data:req.data,
				url:"http://"+allUrl+":8086/elink_postbar/api/reply/addReply",
				success:function(data) {
					if(!data)return;
						$scope.$apply(function(){
							if(data.code==0) {
								alert("提交回答成功！");
								getinfor();					
							}else {
								alert(data.msg);
							}
						});					
					},
				error:function() {
					alert("我都是没问题");
				}
			});			
		};
		
		//采纳按钮提示
		$scope.isShow = function($index) {
			var index = $index;
			var thes = $(".adoptPro");
			$(thes[index]).show();
		};
		$scope.noShow = function($index) {
			var index = $index;
			var thes = $(".adoptPro");
			$(thes[index]).hide();
		};
		
		//采纳请求
		$scope.adopted = function($index) {
			var replyid = $scope.replylist[$index].replyId;

			var req = {
				type:'GET',
				uri:'updateSelect',
				data:{
					replyId:replyid
				}
			};
			httpServices.addpraiseHttpService(req).success(function(data){
				if(!data)return;
				$scope.$apply(function(){
					if(data.code==0) {
						$(".adoptbox").show();
						$(".adopt").hide();
						setTimeout(function(){
							$(".adoptbox").hide();
							
							//刷新数据
							getinfor();
						},1500);
					}else {
						alert(data.msg);
					}
				});
			}).error(function(){
				console.log("采纳请求失败");
			});
		};		
		
		//关闭-采纳成功提示box
		$scope.adoptboxHide = function() {
			$(".adoptbox").hide();
			getinfor();
		};
		
		//关闭问题提示盒子的showhide
		$scope.hide = function() {
			$(".flags-none-div").hide();
		};
		$scope.show = function() {
			$(".flags-none-div").show();
		};

		//我要回答
		$(".otherbox").hide();
		var showhide = 0;
		$scope.IDo = function() {
			showhide++;
			if(showhide == 1) {
				$scope.bottomText2 = true;
				$scope.bottomText = false;
				$(".otherbox").show();
				$(".l-id-isay").html("取消回答");
			} else {
				$scope.bottomText2 = false;
				$scope.bottomText = true;
				showhide = 0;
				$(".otherbox").hide();
				$(".l-id-isay").html("我要回答");
			}
		};

		//主键id，当前页，每页显示条数，是否全部显示
		var pbid = $stateParams.postbarId,
			pageno = 1,
			pagesize = 10,
			boon = false;
		
		//百问百答详情 收藏接口
		$scope.collection = function() {
            moreHttpServices.updateCollection(pbid,10).success(function(response){
                if(response.code == 0){
                    $scope.$apply(function(){
						$(".isay-div1").css({
							'margin-left': '20px',
							'background': 'url("them/img/collectioned.png") 0px 8px no-repeat',
							'background-size': '16px 15px'
						});
						$("#collectionisok").show();
                    })
                }
                if(response.code == 112){
                    $scope.$apply(function(){
						$(".isay-div1").css({
							'margin-left': '20px',
							'background': 'url("them/img/isaydiv1.png") 0px 8px no-repeat',
							'background-size': '16px 15px'
						});	
                    })
                }
            }).error(function(){
				alert('收藏请求失败');
            })			
		};
		
		$scope.Entrance = {
			"pbId": pbid,
			"pageNo": pageno,
			"pageSize": pagesize,
			"all": boon
		}
		var info = [];
		
		//百问百答详情
		getinfor();
		function getinfor(){
			var req = {
				type:'GET',
				uri:'getInfo',
				data:{
					pbId:$scope.Entrance.pbId,
					pageNo:pageno,
					pageSize:pagesize,
					all:boon
				}
			};
			httpServices.postbarlistHttpService(req).success(function(data){
				if(!data)return;
				$scope.$apply(function(){
					if(data.code==0) {
						console.log(data);
						var onelist = data.data.list;
						var alist = data.data.list;
						//判断是否解决
						var solve = data.data.info.solve;
						if(solve == 2){
							$(".priceput").hide();
							$(".priceput2").show().html("已解决");
							$(".flag").show();
							$scope.bottomText3 = false;
							$scope.myli1 = false;
							$scope.myli2 = false;
							$scope.otherli = true;
							$scope.bottomText = true;
						}
						//为后面 自己回答 置顶 需求  先创建两条数据
						var myswer = [];
						var otherswer = [];
						for(i in alist) {
							if(alist[i].userId == $rootScope.userLoginInfo.data.userId){
								myswer.push(alist[i])
							}else if(alist[i].userId !== $rootScope.userLoginInfo.data.userId){
								otherswer.push(alist[i])
							}
						}
						$scope.myswer = myswer;//当前用户的回答
						$scope.otherswer = otherswer;//其他用户的回答
						//是否收藏
						if(data.data.info.collectionFlag == true){
							$(".isay-div1").css({
								'margin-left': '38px',
								'background': 'url("them/img/collectioned.png") 0px 8px no-repeat',
								'background-size': '16px 15px'
							});							
						}else {
							$(".isay-div1").css({
								'margin-left': '38px',
								'background': 'url("them/img/isaydiv1.png") 0px 8px no-repeat',
								'background-size': '16px 15px'
							});								
						}
						//全局保存初始list数据
						$scope.replylist = data.data.list;
						
						var info = data.data.info;
						var list = data.data.list;
						var top = data.data.top.tcName;
						var sec = data.data.sec.scName;
						
						//是否有悬赏
						if(info.price == null||info.price == 0) {
							$(".priceput").hide();
						} else {
							$scope.price = info.price;
						}						
						//头部路径
						$scope.nav3 = {
							title: info.title
						};
						//判断问题是否关闭
						if(info.closeStatus == 1) {
							$(".flags-none").unbind("click");
							$(".flags-none-div").hide();
							$(".flags-none").html("问题已关闭").css({
								"color": "#999",
								"border": "none"
							});
							$(".answer-input-box").hide();
							$(".answer-paging-box").css({
								"margin-bottom":"20px"
							})
						}else {
							$(".flags-none").bind("click");
							$(".answer-paging-box").css({
								"margin-bottom":"0"
							})							
						}
						
						//局部保存数据
						var Pagination = data.data.Pagination;
						$scope.allcount = Pagination.totalCount;
						$scope.labels = info.labList;
						$scope.content = info.content;
						$scope.h1 = info.title;
						$scope.time = info.time;
						$scope.user = info.nickname;
						$scope.brow = info.browseCount;
						$scope.theUserId = info.userId;
						$scope.postbarId = info.postbarId;

						if(data.data.list.length == 0) {
							$(".answer-top").html("暂时还没有人回答");
							
						}else {
							$(".answer-top").html("回答");
							$(".answer-paging-box").show();
						}


						//判断是否是本人提的问题
						if(info.flag == false) {//别人的问题
							
							if(info.closeStatus == 1) {
								$(".flag").hide();
								$(".flags").show();
								$scope.bottomText = false;
								$(".flags-none").unbind();
								$(".flags-none-div").hide();
								$(".flags-none").html("问题已关闭").css({
									"color": "#999",
									"border": "none"
								});
								$(".answer-input-box").hide();
								$(".answer-paging-box").css({
									"margin-bottom":"20px"
								})
							}else {
								$(".flag").show();
								$(".flags").hide();
								$(".flags-none").bind();
								$(".answer-paging-box").css({
									"margin-bottom":"0"
								})
								$scope.bottomText = true;
								
								$scope.wtf = false;
								$scope.wtf2 = true;
								$scope.wtf3 = false;
							}
							
//							$(".flag").show();
//							$(".flags").hide();
//							$scope.wtf = false;
//							$scope.wtf2 = true;
//							$scope.wtf3 = false;
							$(".answer-paging-box").hide();
							// 该问题下是否有我的回答 有的话就置顶 没有的话就 正常遍历
							if($scope.myswer.length == 1) {
								if($scope.myswer[0].bestAnswer == 1) {
									$(".l-id-isay").hide();
									$scope.bottomText = false;
									$scope.bottomText2 = false;
									$scope.bottomText3 = false;
									$scope.myli1 = false;
									if($scope.otherswer.length == 0) {
										$scope.myli2 = false;
									}else {
										$scope.myli2 = true;
									}
									$scope.otherli = false;
									$scope.editorContent = $scope.myswer[0].content;
									$scope.replyId = $scope.myswer[0].replyId;									
								}else {
									$(".l-id-isay").hide();
									$scope.bottomText = false;
									$scope.bottomText2 = false;
									$scope.bottomText3 = true;
									$scope.myli1 = true;
									if($scope.otherswer.length == 0) {
										$scope.myli2 = false;
									}else {
										$scope.myli2 = true;
									}
									$scope.otherli = false;
									$scope.editorContent = $scope.myswer[0].content;
									$scope.replyId = $scope.myswer[0].replyId;									
								}
							}else {//正常遍历
//								$(".flag").show();
								$scope.bottomText3 = false;
								$scope.myli1 = false;
								$scope.myli2 = false;
								$scope.otherli = true;
//								$scope.bottomText = true;
							}
							$scope.nobest = [];
							$scope.best = [];							
							
							//点赞  最佳回答 回复数量相关
							if(!list) {
								return false;
							} else {
								for(i in list) {
									if(list[i].bestAnswer == 1) {
										$scope.other = $scope.allcount-1;
										if(list[i].praise == null) {
											list[i].praise = 0;
										} else if(list[i].boo == null) {
											list[i].boo = 0;
										}
										$scope.best.push(list[i]);
									} else if(list[i].bestAnswer == 0) {
										$scope.nobest.push(list[i]);
										$scope.other = $scope.allcount;
									}
								}
								if($scope.best.length == 0) {
									$(".best-answer").hide();
								} else {
									if(($scope.allcount-1)>=0) {
										
									}else {
										$scope.other = 0;
									}
								};
							}							
						} else {//本人的问题
							$(".flag").hide();
							$(".flags").show();
							$scope.wtf = true;
							$scope.wtf2 = false;
							$scope.wtf3 = false;
							$scope.bottomText = false;
							$scope.bottomText3 = false;
							$scope.myli1 = false;
							$scope.myli2 = false;
							$scope.otherli = true;
							$scope.nobest = [];
							$scope.best = [];
							
							//点赞  最佳回答 回复数量相关
							if(!list) {
								return false;
							} else {
								for(i in list) {
									if(list[i].bestAnswer == 1) {
										$scope.other = $scope.allcount-1;
										if(list[i].praise == null) {
											list[i].praise = 0;
										} else if(list[i].boo == null) {
											list[i].boo = 0;
										}
										$scope.best.push(list[i]);
									} else if(list[i].bestAnswer == 0) {
										$scope.nobest.push(list[i]);
										$scope.other = $scope.allcount;
									}
								}
								if($scope.best.length == 0) {
									$(".best-answer").hide();
								} else {
									if(($scope.allcount-1)>=0) {
										
									}else {
										$scope.other = 0;
									}
								};
							}
						}
						//分页
						if(list.length <= 10) {
							$(".paging-btn").hide();
						} else if(list.length > 10) {
							$(".paging-btn").show();
						};
					} else {
						console.log(data.msg);
					}						
				});
			}).error(function(){
				alert("百问百答详情请求失败")
			});	
		};
		
		//获取相关问题
		getquestion();
		//判断盒子颜色
		$scope.getStyle  =function(oo,tt){
			if(oo==0){
				return {
					border:"1px solid #ec251f",
					color:"#ec251f"
				};
			}else if(oo>0&&tt==1){
				return {
					border:"1px solid #35b558",
					color:"#35b558"
				};
			}else if(oo>0&&tt==2){
				return {
					border:"1px solid #ccc",
					color:"#ccc"
				};				
			}	
		};
		//取消-编辑交替
		$scope.myflag = true;
		$scope.myswerbtn = function() {
			if($scope.myflag == true) {
				$("#myswer-btn").html("编辑");
				$scope.bottomText3 = false;
				$scope.myflag = false;
			}else {
				$("#myswer-btn").html("取消");
				$scope.bottomText3 = true;
				$scope.myflag = true;
			}
		};
		
		//获取相关问题
		function getquestion() {
			var req = {
				type:'GET',
				uri:'getRelevant',
				data:{
					pbId:$stateParams.postbarId,
					count:10
				}
			};
			httpServices.postbarlistHttpService(req).success(function(data){
				if(!data)return;
				if(data.code == 0) {
					$scope.$apply(function() {
						$scope.otherdata = data.data;
						$scope.gotoxq = function(the) {
							$state.go("eng.postbardeta",{postbarId:the});
						};
					});
				} else {
					alert(data.msg);
				}			
			}).error(function(){
				alert("获取相关问题请求失败");
			});
		};

		//相关问题左边小正方形

		//点赞 
		$scope.type1 = function(replyid,$index,event) {
			var src = event.srcElement || event.target;
			var num = $(src).html();
			var req = {
				type:'POST',
				uri:'addPraise',
				data:{
					"praiseId": "",
					"replyId": replyid,
					"type": 1,
					"userId": "",
					"praiseTime": ""					
				}
			};
			httpServices.addpraiseHttpService(req).success(function(data){
				if(!data)return;
				$scope.$apply(function(){
					if(data.code==0) {
						$(src).html(parseInt(num)+1);
					}else if(data.code == 117){
						$(src).html(parseInt(num)-1);
					}else {
						alert(data.msg);
					}
				});
			}).error(function(data){
				alert("点赞请求失败");
			});
		};
		
		//点差
		$scope.type2 = function(replyid,$index,event) {
			var src = event.srcElement || event.target;
			var num = $(src).html();
			
			var req = {
				type:'POST',
				uri:'addPraise',
				data:{
					"praiseId": "",
					"replyId": replyid,
					"type": 2,
					"userId": "",
					"praiseTime": ""					
				}
			};
			httpServices.addpraiseHttpService(req).success(function(data){
				if(!data)return;
				$scope.$apply(function(){
					if(data.code==0) {
						$(src).html(parseInt(num)+1);
					}else if(data.code == 117){
						$(src).html(parseInt(num)-1);
					}else {
						alert(data.msg);
					}
				});
			}).error(function(data){
				alert("差评请求失败");
			});
		};

	})
})